home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / wc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-06  |  4.2 KB  |  173 lines

  1. /* wc - count lines, words and characters       Author: David Messer */
  2.  
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "amigawildcard.h"
  7.  
  8. /*
  9.  *
  10.  *      Usage:  wc [-lwc] [names]
  11.  *
  12.  *              Flags:
  13.  *                      l - count lines.
  14.  *                      w - count words.
  15.  *                      c - count characters.
  16.  *
  17.  *              Flags l, w, and c are default.
  18.  *              Words are delimited by any non-alphabetic character.
  19.  *
  20.  *  Released into the PUBLIC-DOMAIN 02/10/86
  21.  *
  22.  *      If you find this program to be of use to you, a donation of
  23.  *      whatever you think it is worth will be cheerfully accepted.
  24.  *
  25.  *      Written by: David L. Messer
  26.  *                              P.O. Box 19130, Mpls, MN,  55119
  27.  *      Program (heavily) modified by Andy Tanenbaum
  28.  *      Program modified by Jean-Francois Fabre (Added amiga wildcard support)
  29.  */
  30.  
  31.  
  32. int lflag;                      /* Count lines */
  33. int wflag;                      /* Count words */
  34. int cflag;                      /* Count characters */
  35.  
  36. long lcount;                    /* Count of lines */
  37. long wcount;                    /* Count of words */
  38. long ccount;                    /* Count of characters */
  39.  
  40. long ltotal;                    /* Total count of lines */
  41. long wtotal;                    /* Total count of words */
  42. long ctotal;                    /* Total count of characters */
  43.  
  44. int main  (int argc, char **argv);
  45. void count  (FILE *f);
  46. void usage  (void);
  47.  
  48. int main(argc, argv)
  49. /* [<][>][^][v][top][bottom][index][help] */
  50. int argc;
  51. char *argv[];
  52. {
  53.   int k;
  54.   char *cp;
  55.   int tflag, files;
  56.   t_strlist strl;
  57.   int argstart;
  58.  
  59.   /* Get flags. */
  60.   files = argc - 1;
  61.   k = 1;
  62.   cp = argv[1];
  63.   if (argc > 1 && *cp++ == '-') {
  64.         files--;
  65.         k++;                    /* points to first file */
  66.         while (*cp != 0) {
  67.                 switch (*cp) {
  68.                     case 'l':   lflag++;        break;
  69.                     case 'w':   wflag++;        break;
  70.                     case 'c':   cflag++;        break;
  71.                     default:    usage();
  72.                 }
  73.                 cp++;
  74.         }
  75.   }
  76.  
  77.   /* If no flags are set, treat as wc -lwc. */
  78.   if (!lflag && !wflag && !cflag) {
  79.         lflag = 1;
  80.         wflag = 1;
  81.         cflag = 1;
  82.   }
  83.  
  84.   /* start amiga wildcard */
  85.   fill_list(argv,k,argc,&strl);
  86.   argstart=k;
  87.   files=strl.len-argstart;
  88.   print_list(&strl);
  89.   /* end amiga wildcard */
  90.  
  91.   /* Process files. */
  92.   tflag = files >= 2;           /* set if # files > 1 */
  93.  
  94.   /* Check to see if input comes from std input. */
  95.   if (k >= argc) {
  96.         count(stdin);
  97.         if (lflag) printf(" %6ld", lcount);
  98.         if (wflag) printf(" %6ld", wcount);
  99.         if (cflag) printf(" %6ld", ccount);
  100.         printf(" \n");
  101.         fflush(stdout);
  102.         clear_list(&strl);
  103.         return(0);
  104.   }
  105.  
  106.   /* There is an explicit list of files.  Loop on files. */
  107.   while (k < (strl.len+argstart)) {
  108.         FILE *f;
  109.         char *infile;
  110.  
  111.         infile=pop_elt(k-argstart,&strl);
  112.  
  113.         if ((f = fopen(infile, "r")) == NULL) {
  114.                 fprintf(stderr, "wc: cannot open %s\n", infile);
  115.         } else {
  116.                 count(f);
  117.                 if (lflag) printf(" %6ld", lcount);
  118.                 if (wflag) printf(" %6ld", wcount);
  119.                 if (cflag) printf(" %6ld", ccount);
  120.                 printf(" %s\n", infile);
  121.                 fclose(f);
  122.         }
  123.         k++;
  124.   }
  125.  
  126.   if (tflag) {
  127.         if (lflag) printf(" %6ld", ltotal);
  128.         if (wflag) printf(" %6ld", wtotal);
  129.         if (cflag) printf(" %6ld", ctotal);
  130.         printf(" total\n");
  131.   }
  132.   fflush(stdout);
  133.  
  134.   clear_list(&strl);
  135.   return(0);
  136. }
  137.  
  138. void count(f)
  139. /* [<][>][^][v][top][bottom][index][help] */
  140. FILE *f;
  141. {
  142.   register int c;
  143.   register int word = 0;
  144.  
  145.   lcount = 0;
  146.   wcount = 0;
  147.   ccount = 0L;
  148.  
  149.   while ((c = getc(f)) != EOF) {
  150.         ccount++;
  151.  
  152.         if (isspace(c)) {
  153.                 if (word) wcount++;
  154.                 word = 0;
  155.         } else {
  156.                 word = 1;
  157.         }
  158.  
  159.         if (c == '\n' || c == '\f') lcount++;
  160.   }
  161.   ltotal += lcount;
  162.   wtotal += wcount;
  163.   ctotal += ccount;
  164. }
  165.  
  166. void usage()
  167. /* [<][>][^][v][top][bottom][index][help] */
  168. {
  169.   fprintf(stderr, "Usage: wc [-lwc] [name ...]\n");
  170.   exit(1);
  171. }
  172. /* [<][>][^][v][top][bottom][index][help] */
  173.